package com.wjx.servlet;


import com.wjx.baseservlet.ViewBaseServlet;
import com.wjx.po.Parkingspaces;
import com.wjx.service.IParkingspacesBiz;
import com.wjx.service.ParkingspacesBizImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "emp", urlPatterns = "/emp")
public class ParkingspacesServlet extends ViewBaseServlet {
    //引入业务类
    public IParkingspacesBiz ip = new ParkingspacesBizImpl();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //编码设置
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        //获取业务方法参数
        String method = request.getParameter("method");
        if (method.equals("addJump")) {
            addJump(request, response);
        } else if (method.equals("findAll")) {
            findAll(request, response);
        } else if (method.equals("add")) {
            add(request, response);
        } else if (method.equals("delete")) {
            delete(request, response);
        } else if (method.equals("getById")) {
            getById(request, response);
        } else if (method.equals("update")) {
            update(request, response);
        }
    }

    private void addJump(HttpServletRequest request, HttpServletResponse response) throws IOException {
        super.processTemplate("addemp", request, response);
    }

    private void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Parkingspaces> emps = ip.findAll();
        request.setAttribute("emps", emps);
        super.processTemplate("emplist", request, response);
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取页面参数
        String owner = request.getParameter("owner");
        String phone = request.getParameter("phone");
        String street = request.getParameter("street");
        String sparkingnum = request.getParameter("parkingnum");
        int parkingnum = Integer.parseInt(sparkingnum);
        String starttime = request.getParameter("starttime");
        String endtime = request.getParameter("endtime");
        String sprice = request.getParameter("price");
        Double price = Double.valueOf(sprice);
        //组合对象
        Parkingspaces ps = new Parkingspaces(owner, phone, street, parkingnum, starttime, endtime, price);
        //调用业务层
        boolean result = ip.add(ps);
        String str = "";
        if (result == true) {
            str = "添加成功!";
        } else {
            str = "添加失败!";
        }
        request.setAttribute("str", str);
        request.getRequestDispatcher("emp?method=findAll").forward(request, response);
    }

    private void getById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        int psid = Integer.parseInt(request.getParameter("psid"));
        Parkingspaces emp = ip.findById(psid);
        request.setAttribute("emp", emp);
        super.processTemplate("updateemp", request, response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String spsid = request.getParameter("psid");
        int psid = Integer.parseInt(spsid);
        boolean result = ip.delete(psid);
        String str = "";
        if (result == true) {
            str = "删除成功!";
        } else {
            str = "删除失败!";
        }
        request.setAttribute("str", str);
        //转发到修改页面
        request.getRequestDispatcher("emp?method=findAll").forward(request, response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取页面参数
        String spsid = request.getParameter("psid");
        int psid = Integer.parseInt(spsid);
        String owner = request.getParameter("owner");
        String phone = request.getParameter("phone");
        String street = request.getParameter("street");
        String sparkingnum = request.getParameter("parkingnum");
        int parkingnum = Integer.parseInt(sparkingnum);
        String starttime = request.getParameter("starttime");
        String endtime = request.getParameter("endtime");
        String sprice = request.getParameter("price");
        Double price = Double.valueOf(sprice);
        //组合对象
        Parkingspaces ps = new Parkingspaces(psid, owner, phone, street, parkingnum, starttime, endtime, price);
        //调用业务层
        boolean result = ip.update(ps);
        String str = "";
        if (result == true) {
            str = "修改成功!";
        } else {
            str = "修改失败!";
        }
        request.setAttribute("str", str);
        request.getRequestDispatcher("emp?method=findAll").forward(request, response);
    }


}
